<?php

namespace App\Http\Controllers\Admin\Statistics;

use App\Http\Controllers\Controller;
use App\Models\City;
use App\Models\MemberShip;
use App\Models\Order;
use App\Models\User;
use App\Models\UserAddress;
use App\Models\UserLog;
use App\Models\UserRecharge;
use App\Traits\Controllers\AdminTraits;
use App\Transformers\Statistics\UserLogTransformer;
use Carbon\Carbon;
use Dingo\Api\Http\Response;
use Illuminate\Http\Request;

class UserLogController extends Controller
{
    use AdminTraits;

    public $model = UserLog::class;

    public $transformer = UserLogTransformer::class;

    //用户统计
    public function usertop(Request $request)
    {

        $this->formatIndexQuery($request);
        $start = $request->input('start') ?? '1997-1-1';
        $end = $request->input('end') ?? '2222-1-1';

        $data = User::query()->pimp()
            ->pluck('id');

        //用户支付的总金额
        $total_amount = Order::query()->pimp()
            ->whereBetween('created_at', [$start, $end])
            ->sum('pay_price');

        //支付人数
        $number = Order::query()->pimp()
            ->where('paid', 1)
            ->with('user:id')
            ->whereBetween('created_at', [$start, $end])
            ->count();

        //新增用户数
        $time = Carbon::now()->subDays(14)
            ->toDateTimeString();

        $time1 = [
            'created_at' => Carbon::now(),
            'end_at' => Carbon::now()
                ->addDays(29),
        ];

        $usernum = User::query()->pimp()
            ->where('created_at', '>=', $time)
            ->whereBetween('created_at', [$start, $end])
            ->count();

        //客单价

        if ($number == 0) {
            $customer = 0;
        } else {
            $customer = sprintf("%01.2f", $total_amount / $number);
        }


        //访客数
        $visitor_num = $this->getQuery()->pimp()
            ->whereBetween('created_at', [$start, $end])
            ->count();


        //累计成交用户数
        $deal_num = Order::query()->pimp()
            ->where('paid', 1)
            ->whereIn('user_id', $data)
            ->wherebetween('created_at', $time1)
            ->whereBetween('created_at', [$start, $end])
            ->count();

        //浏览量
        $browse_num = $this->getQuery()->pimp()
            ->whereBetween('created_at', [$start, $end])
            ->count();

        //成交用户数
        $order_num = Order::query()->pimp()
            ->where('paid', 1)
            ->whereBetween('created_at', [$start, $end])
            ->count();

        //付费会员数
        $visitor = User::query()->pimp()
            ->where('is_member', 1)
            ->whereBetween('created_at', [$start, $end])
            ->count();

        //充值用户数
        $recharge_num = UserRecharge::query()->pimp()
            ->where('is_paid', 1)
            ->whereBetween('created_at', [$start, $end])
            ->count();

        //累计用户
        $user_num = User::query()->pimp()
            ->whereBetween('created_at', [$start, $end])
            ->count();

        //累计付费会员数
        $pay_num = MemberShip::query()->pimp()
            ->wherebetween('created_at', $time1)
            ->whereBetween('created_at', [$start, $end])
            ->whereIn('type', ['month', 'quarter', 'year', 'ever',])
            ->count();

        //累计充值用户数
        $cumulative_num = UserRecharge::query()
            ->pimp()
            ->where('is_paid', 1)
            ->wherebetween('created_at', $time1)
            ->whereBetween('created_at', [$start, $end])
            ->count();

        //访客-支付转化率
        if ($visitor_num == 0) {
            $payment = 0;
        } else {
            $payment = sprintf("%01.2f", $deal_num / $visitor_num * 100) . '%';
        }

        $count = [

            'name1' => '访客数',
            'visitor_num' => $visitor_num,

            'name2' => '浏览量',
            'browse_num' => $browse_num,

            'name3' => '新增用户数',
            'register_num' => $usernum,

            'name4' => '成交用户数',
            'order_num' => $order_num,

            'name5' => '访客-支付转化率',
            'payment' => $payment,

            'name6' => '付费会员数',
            'visitor' => $visitor,

            'name7' => '充值用户数',
            'recharge_num' => $recharge_num,

            'name8' => '客单价',
            'list_num' => $customer,

            'name9' => '累计用户',
            'user_num' => $user_num,

            'name10' => '累计付费会员数',
            'pay_num' => $pay_num,

            'name11' => '累计充值用户数',
            'cumulative_num' => $cumulative_num,

            'name12' => '累计成交用户数',
            'deal_num' => $deal_num,

        ];

        return $this->response->array($count);
    }

    //用户地域分布人数
    public function addresses()
    {
        $data = $this->getQuery()->pimp()
            ->selectRaw('city_id, sum(user_num) as user_num ,sum(register_num) as register_num,sum(visitor_num) as visitor_num, sum(amount) as amount')
            ->GroupBy('city_id')
            ->OrderBy('city_id', 'asc')
            ->with('city:id,name')
            ->get();

        return $this->response->collection($data, new UserLogTransformer());
    }


    //用户性别比例
    public function sex()
    {

        //总用户数
        $data = User::query()->pimp()
            ->count();

        //男生人数
        $schoolboy = User::query()->pimp()
            ->where('sex', 0)
            ->count();

        //女生人数
        $girl_student = User::query()->pimp()
            ->where('sex', 1)
            ->count();

        //未知的人数
        $unknown = ($data - ($schoolboy + $girl_student));

        $count = [

            'name1' => '总用户数',
            'data' => $data,

            'name2' => '男生',
            'male' => $schoolboy,
            'male1' => sprintf("%01.2f",
                    $schoolboy / $data * 100) . '%',


            'name3' => '女生',
            'female' => $girl_student,
            'female1' => sprintf("%01.2f",
                    $girl_student / $data * 100) . '%',


            'name4' => '未知',
            'unknown1' => $unknown,
            'unknown2' => sprintf("%01.2f",
                    $unknown / $data * 100) . '%',

        ];

        return $this->response->array($count);
    }

    public function growth(Request $request)
    {

        //今年的环比增长的时间
        $this_year = [
            'data' => Carbon::today()->subMonths(8)->subDays(21)
                ->toDateTimeString(),
            'data1' => Carbon::today()->addMonths(3)->addDays(9)
                ->toDateTimeString(),
        ];
        //上一年的环比增长的时间
        $last_year = [
            'data' => Carbon::today()->subYears()->subMonths(8)->subDays(21)
                ->toDateTimeString(),
            'data1' => Carbon::today()->subYears()->addMonths(3)->addDays(9)
                ->toDateTimeString(),
        ];
        //本期的
        //先处理传过来的搜索数据
        $this->formatIndexQuery($request);
        $start = $request->input('start') ?? '1997-1-1';
        $end = $request->input('end') ?? '2222-1-1';

        $data = User::query()->pimp()
            ->pluck('id');

        //用户支付的总金额
        $total_amount = Order::query()->pimp()
            ->whereBetween('created_at', [$start, $end])
            ->whereBetween('created_at', $this_year)
            ->sum('pay_price');

        //支付人数
        $number = Order::query()->pimp()
            ->where('paid', 1)
            ->with('user:id')
            ->whereBetween('created_at', [$start, $end])
            ->whereBetween('created_at', $this_year)
            ->count();

        //新增用户数
        $time = Carbon::now()->subDays(14)
            ->toDateTimeString();

        $time1 = [
            'created_at' => Carbon::now(),
            'end_at' => Carbon::now()
                ->addDays(29),
        ];

        $usernum = User::query()->pimp()
            ->where('created_at', '>=', $time)
            ->whereBetween('created_at', [$start, $end])
            ->whereBetween('created_at', $this_year)
            ->count();

        //客单价
        if ($number == 0) {
            $customer = 0;
        } else {
            $customer = sprintf("%01.2f", $total_amount / $number);
        }


        //访客数
        $visitor_num = $this->getQuery()->pimp()
            ->whereBetween('created_at', $this_year)
            ->whereBetween('created_at', [$start, $end])
            ->count();


        //累计成交用户数
        $deal_num = Order::query()->pimp()
            ->where('paid', 1)
            ->whereIn('user_id', $data)
            ->wherebetween('created_at', $time1)
            ->whereBetween('created_at', [$start, $end])
            ->whereBetween('created_at', $this_year)
            ->count();

        //浏览量
        $browse_num = $this->getQuery()->pimp()
            ->whereBetween('created_at', [$start, $end])
            ->whereBetween('created_at', $this_year)
            ->count();

        //成交用户数
        $order_num = Order::query()->pimp()
            ->where('paid', 1)
            ->whereBetween('created_at', [$start, $end])
            ->whereBetween('created_at', $this_year)
            ->count();

        //付费会员数
        $visitor = User::query()->pimp()
            ->where('is_member', 1)
            ->whereBetween('created_at', [$start, $end])
            ->whereBetween('created_at', $this_year)
            ->count();

        //充值用户数
        $recharge_num = UserRecharge::query()->pimp()
            ->where('is_paid', 1)
            ->whereBetween('created_at', [$start, $end])
            ->whereBetween('created_at', $this_year)
            ->count();

        //累计用户
        $user_num = User::query()->pimp()
            ->whereBetween('created_at', [$start, $end])
            ->whereBetween('created_at', $this_year)
            ->count();

        //累计付费会员数
        $pay_num = MemberShip::query()->pimp()
            ->wherebetween('created_at', $time1)
            ->whereBetween('created_at', [$start, $end])
            ->whereIn('type', ['month', 'quarter', 'year', 'ever',])
            ->whereBetween('created_at', $this_year)
            ->count();

        //累计充值用户数
        $cumulative_num = UserRecharge::query()
            ->pimp()
            ->where('is_paid', 1)
            ->wherebetween('created_at', $time1)
            ->whereBetween('created_at', [$start, $end])
            ->whereBetween('created_at', $this_year)
            ->count();

        //访客-支付转化率
        if ($visitor_num == 0) {
            $payment = 0;
        } else {
            $payment = round($deal_num / $visitor_num, 2);
        }

        //上一年的
        //用户支付的总金额
        $total_amount1 = Order::query()->pimp()
            ->whereBetween('created_at', [$start, $end])
            ->whereBetween('created_at', $last_year)
            ->sum('pay_price');

        //支付人数
        $number1 = Order::query()->pimp()
            ->where('paid', 1)
            ->with('user:id')
            ->whereBetween('created_at', [$start, $end])
            ->whereBetween('created_at', $last_year)
            ->count();


        $usernum1 = User::query()->pimp()
            ->where('created_at', '>=', $time)
            ->whereBetween('created_at', [$start, $end])
            ->whereBetween('created_at', $last_year)
            ->count();

        //客单价
        if ($number1 == 0) {
            $customer1 = 0;
        } else {
            $customer1 = round($total_amount1 / $number1, 2);
        }


        //访客数
        $visitor_num1 = $this->getQuery()->pimp()
            ->whereBetween('created_at', $last_year)
            ->whereBetween('created_at', [$start, $end])
            ->count();


        //累计成交用户数
        $deal_num1 = Order::query()->pimp()
            ->where('paid', 1)
            ->whereIn('user_id', $data)
            ->wherebetween('created_at', $time1)
            ->whereBetween('created_at', [$start, $end])
            ->whereBetween('created_at', $last_year)
            ->count();

        //浏览量
        $browse_num1 = $this->getQuery()->pimp()
            ->whereBetween('created_at', [$start, $end])
            ->whereBetween('created_at', $last_year)
            ->count();

        //成交用户数
        $order_num1 = Order::query()->pimp()
            ->where('paid', 1)
            ->whereBetween('created_at', [$start, $end])
            ->whereBetween('created_at', $last_year)
            ->count();

        //付费会员数
        $visitor1 = User::query()->pimp()
            ->where('is_member', 1)
            ->whereBetween('created_at', [$start, $end])
            ->whereBetween('created_at', $last_year)
            ->count();

        //充值用户数
        $recharge_num1 = UserRecharge::query()->pimp()
            ->where('is_paid', 1)
            ->whereBetween('created_at', [$start, $end])
            ->whereBetween('created_at', $last_year)
            ->count();

        //累计用户
        $user_num1 = User::query()->pimp()
            ->whereBetween('created_at', [$start, $end])
            ->whereBetween('created_at', $last_year)
            ->count();

        //累计付费会员数
        $pay_num1 = MemberShip::query()->pimp()
            ->wherebetween('created_at', $time1)
            ->whereBetween('created_at', [$start, $end])
            ->whereIn('type', ['month', 'quarter', 'year', 'ever',])
            ->whereBetween('created_at', $last_year)
            ->count();

        //累计充值用户数
        $cumulative_num1 = UserRecharge::query()
            ->pimp()
            ->where('is_paid', 1)
            ->wherebetween('created_at', $time1)
            ->whereBetween('created_at', [$start, $end])
            ->whereBetween('created_at', $last_year)
            ->count();

        //访客-支付转化率
        if ($visitor_num1 == 0) {
            $payment1 = 0;
        } else {
            $payment1 = round(($deal_num1 / $visitor_num1), 2);
        }
        //环比增长率
        if ($visitor_num1 == 0) {
            $visitor_num2 = 0 . '%';
        } else {
            $visitor_num2 = round(($visitor_num - $visitor_num1) / $visitor_num1, 2) * 100 . '%';
        }

        if ($browse_num1 == 0) {
            $browse_num2 = 0 . '%';
        } else {
            $browse_num2 = round(($browse_num - $browse_num1) / $browse_num1, 2) * 100 . '%';
        }

        if ($usernum1 == 0) {
            $usernum2 = 0 . '%';
        } else {
            $usernum2 = round(($usernum - $usernum1) / $usernum1, 2) * 100 . '%';
        }

        if ($order_num1 == 0) {
            $order_num2 = 0 . '%';
        } else {
            $order_num2 = round(($order_num - $order_num1) / $order_num1, 2) * 100 . '%';
        }

        if ($payment1 == 0) {
            $payment2 = 0 . '%';
        } else {
            $payment2 = round(($payment - $payment1) / $payment1, 2) * 100 . '%';
        }

        if ($visitor1 == 0) {
            $visitor2 = 0 . '%';
        } else {
            $visitor2 = round(($visitor - $visitor1) / $visitor1, 2) * 100 . '%';
        }

        if ($recharge_num1 == 0) {
            $recharge_num2 = 0 . '%';
        } else {
            $recharge_num2 = round(($recharge_num - $recharge_num1) / $recharge_num1, 2) * 100 . '%';

        }

        if ($customer1 == 0) {
            $customer2 = 0 . '%';
        } else {
            $customer2 = round(($customer - $customer1) / $customer1, 2) * 100 . '%';
        }

        if ($user_num1 == 0) {
            $user_num2 = 0 . '%';
        } else {
            $user_num2 = round(($user_num - $user_num1) / $user_num1, 2) * 100 . '%';
        }

        if ($pay_num1 == 0) {
            $pay_num2 = 0 . '%';
        } else {
            $pay_num2 = round(($pay_num - $pay_num1) / $pay_num1, 2) * 100 . '%';
        }


        if ($cumulative_num1 == 0) {
            $cumulative_num2 = 0 . '%';
        } else {
            $cumulative_num2 = round(($cumulative_num - $cumulative_num1) / $cumulative_num1, 2) * 100 . '%';
        }

        if ($deal_num1 == 0) {
            $deal_num2 = 0 . '%';
        } else {
            $deal_num2 = round(($deal_num - $deal_num1) / $deal_num1, 2) * 100 . '%';
        }


        $count = [

            'visitor_num' => $visitor_num2,

            'browse_num' => $browse_num2,

            'register_num' => $usernum2,

            'order_num' => $order_num2,

            'payment' => $payment2,

            'visitor' => $visitor2,

            'recharge_num' => $recharge_num2,

            'list_num' => $customer2,

            'user_num' => $user_num2,

            'pay_num' => $pay_num2,

            'cumulative_num' => $cumulative_num2,

            'deal_num' => $deal_num2,

        ];
        return $this->response->array($count);
    }

    public function chart()
    {
        // 返回30天时间
        for ($i = 0; $i < 30; $i++) {
            $time[] = Carbon::now()->subDays($i)->toDateString();
        }

        //返回30天商品数据
        for ($i = 0; $i < 30; $i++) {
            $start_time = Carbon::now()->startOfDay()->subDays($i);
            $end_time = Carbon::now()->endOfDay()->subDays($i);
            $data[$i] = $this->getQuery()
                ->whereBetween('created_at', [$start_time, $end_time])
                ->selectRaw('SUM(register_num) as register_num,
                            SUM(visitor_num) as visitor_num,
                            SUM(order_num) as order_num,
                            SUM(recharge_num) as recharge_num,
                            SUM(number_num) as number_num'
                )->get();
        }

        //改变数据格式
        foreach ($data as $k => $v) {
            foreach ($v as $a => $b) {
                $arr[0]['data'][] = $b['register_num'];
                $arr[1]['data'][] = $b['visitor_num'];
                $arr[2]['data'][] = $b['order_num'];
                $arr[3]['data'][] = $b['recharge_num'];
                $arr[4]['data'][] = $b['number_num'];
            }
        }

        $arr[0]['name'] = '新增用户数';
        $arr[0]['type'] = 'line';
        $arr[1]['name'] = '访客数';
        $arr[1]['type'] = 'line';
        $arr[2]['name'] = '成交用户数';
        $arr[2]['type'] = 'line';
        $arr[3]['name'] = '充值用户';
        $arr[3]['type'] = 'line';
        $arr[4]['name'] = '新增付费用户数';
        $arr[4]['type'] = 'line';
        $array['series'] = $arr;
        $array['xAxis'] = $time;

        return $this->response->array($array);
    }
}
